﻿using AutoMapper.Internal.Mappers;
using Mes.Pendant.CustomerManagements;
using Mes.Pendant.CustomerManagements.DTO;
using Mes.Pendant.CustomerManagementTable;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Pendant.Web.Controllers
{
    /// <summary>
    /// 控制器
    /// </summary>
    public class CustomerManagementController : Controller
    {
        private readonly ICustomerManagementAppService _customerServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="customerServices"></param>
        public CustomerManagementController(ICustomerManagementAppService customerServices)
        {
            _customerServices = customerServices;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="customerCode"></param>
        /// <param name="customerName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetCustomerManagementInfo(string? customerCode, string? customerName, int page = 1, int limit = 10)
        {
            var list = _customerServices.GetCustomerManagementInfo(customerCode, customerName, page, limit);
            return Json(new { data = list.list, count = list.total, code = 0 });
        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="customerId">主键ID</param>
        /// <returns></returns>
        public JsonResult DeleteCustomerManagementInfo(int customerId)
        {
            var list = _customerServices.DeleteCustomerManagementInfo(customerId);
            return Json(new { code = 1, meg = "success" });
        }
        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>

        public IActionResult Insert()
        {
            return View();
        }
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="customerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult PostCustomerManagementInfo(CreateCustomerManagementDTO customerDto)
        {
            _customerServices.PostCustomerManagementInfo(customerDto);
            return Json(new { code = 1, msg = "success" });
        }
        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(int id)
        {
            ViewBag.id = id;
            return View();  
        }
        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCustomerById(int id)
        {
            var customerlist = _customerServices.GetCustomerById(id);
            return Json(new { data= customerlist });
        }
        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="customerDto"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult PutCustomerManagementInfo(UpdateCustomerManagementDTO customerDto)
        {
            _customerServices.PutCustomerManagementInfo(customerDto);
            return Json(new { code = 1, msg = "success" });
        }
    }
}
